<template>
	<view>
		<navbar title="浏览图片" :isbg="false" :autoBack="true" :opacity='1'></navbar>
		<view class="con">
			<view class="box">
				<u-upload :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" width="180rpx" height="180rpx" name="3" multiple
					:maxCount="10" :previewFullImage="true"></u-upload>
				<u-button text="提交" @click="submit" :loading="btnloading" color="#2A51FA" :customStyle="{
					height:'95rpx',
					width:'638rpx',
					margin:0,
					fontSize:'30rpx',
					borderRadius:'20rpx'
				}"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import datagl from '@/api/_datagl.js'
	export default {
		data() {
			return {
				fileList3: [],
				id: '',
				btnloading:false,
				images:''
			};
		},
		methods: {
			async submit(){
				let imgarr=[]
				this.fileList3.map(t=>{
					imgarr.push(t.fullurl)
				})
				this.images=imgarr.join(',')
				this.btnloading=true
				let res = await datagl.appendCustomerImage({id:this.id,image:this.images})
				if(res.data.code==1){
					uni.$u.toast('提交成功')
				}
				this.btnloading=false
				
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
			
					const result = await this.uploadFilePromise(lists[i].url)
			
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, result))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://88.fuliaoxx.com/api/Common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						header: {
							token: uni.getStorageSync('88ftoken')
						},
						success: (res) => {
							setTimeout(() => {
								// console.log(JSON.parse(res.data))
								if (JSON.parse(res.data).code == 401) {
									uni.showToast({
										title: JSON.parse(res.data).msg,
										icon: 'none'
									})
									setTimeout(() => {
										uni.reLaunch({
											url: '/pages/index/login'
										})
									}, 1000)
								}
								if (JSON.parse(res.data).code == 0) {
									uni.showToast({
										title: JSON.parse(res.data).msg,
										icon: 'none'
									})
									setTimeout(() => {
										resolve({
											status: 'error',
											message: '',
											url: ''
										})
									}, 1000)
							
								}
								if (JSON.parse(res.data).code == 1) {
									resolve({
										status: 'success',
										message: '',
										url: JSON.parse(res.data).data.fullurl,
										fullurl: JSON.parse(res.data).data.url
									})
								}
							}, 1000)
						},
					});
				});
			},
			async getimglist(){
				let res = await datagl.getCustomerDetail({customer_id:this.id})
				if(res.data.code==1){
					if(res.data.data.image){
						let arr=res.data.data.image.split(',')
						arr.map(t=>{
							this.fileList3.push({url:this.URL(t),fullurl:t})
						})
						// console.log(this.fileList3)
					}else{
						this.fileList3=[]
					}
					// let arr = res.data.data.
				}
			}
		},
		onLoad(o) {
			this.id = o.id
			this.getimglist()
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F8F8F8;
	}

	.add {
		font-family: PingFang-SC, PingFang-SC;
		font-weight: bold;
		font-size: 30rpx;
		color: #2A51FA;
		line-height: 30rpx;
	}

	.con {
		padding: 30rpx;

		.box {
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			padding: 30rpx;
		}
	}
</style>